package com.blue.firecat.controller;

import com.alibaba.fastjson.JSON;
import com.blue.firecat.contants.Contants;
import com.blue.firecat.contants.Result;
import com.blue.firecat.entity.Game;
import com.blue.firecat.serivce.GameService;
import com.blue.firecat.utils.controller.BaseController;
import com.blue.firecat.vo.OrderVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.jar.Attributes;

/**
 * @描述
 * @时间 2017/6/23 22:27
 * @作者 肖亦鸣
 */
@Controller
@RequestMapping("game")
public class GameController extends BaseController {
    private static Logger logger = LogManager.getLogger(GameController.class.getName());
    @Autowired
    GameService gameService;

    @ResponseBody
    @RequestMapping(value = "getGames", produces = {"text/json;charset=UTF-8"})
    public String getGames() {
        Result<List<Game>> result = gameService.selectByExample(null);
        logger.info("result:{}", JSON.toJSONString(result));
        return JSON.toJSONString(result.getData());
    }

    @ResponseBody
    @RequestMapping(value = "getGamesNoCount", produces = {"text/json;charset=UTF-8"})
    public String getGamesNoCount() {
        logger.info("result:{}", JSON.toJSONString(Contants.TRADE_GAME_RESULT.getData()));
        return JSON.toJSONString(Contants.TRADE_GAME_RESULT.getData());
    }

    @RequestMapping("gameList")
    public String gameList() {
        Result<List<Game>> result = gameService.selectByExample(null);
        request.setAttribute(Contants.GAMES, result.getData());
        logger.info("result:{}", JSON.toJSONString(result));
        return "game-list";
    }

    @RequestMapping(value = "addGame", method = RequestMethod.POST)
    public String addGame(Game game) {
        logger.info("game:{}", JSON.toJSONString(game));
        game.setOperate(getUserBySession().getName());
        Result<List<Game>> result = gameService.insertSelective(game);
        request.setAttribute(Contants.GAMES, result.getData());
        request.setAttribute(Contants.MESSAGE, result.getMessage());
        logger.info("result:{}", JSON.toJSONString(result));
        return "game-list";
    }

    @ResponseBody
    @RequestMapping(value = "deleteGame", produces = {"text/json;charset=UTF-8"})
    public String deleteGame(Game game) {
        Result<Game> result = gameService.deleteByPrimaryKey(game.getId());
        logger.info("result:{}", JSON.toJSONString(result));
        return JSON.toJSONString(result);
    }
}
